<template>
  <div class="adminfeedback">
    <div class="adminmsg">
      <h2>反馈管理</h2>
      <div class="title block">
        <span>反馈列表</span>
      </div>
      <div v-for="item in list" :key="item.id">
        <div class="info block">
          <div class="msg">
            <h3>{{ item.title }}</h3>
            <span :class="item.status == 1 ? 'status1' : 'status0'">{{ item.status == 1 ? '已处理' : '未处理' }}</span>
            <p>{{ item.contact_name }} {{ item.contact_email }}</p>
            <p>反馈内容：{{ item.content }}</p>
            <p>回复内容：{{ item.reply }}</p>
          </div>
          <div>
            <el-button @click="remoke(item.id)" type="text" size="small">删除反馈</el-button>
            <el-button @click="reply(item.id)" type="text" size="small">回复反馈</el-button>
          </div>
        </div>
      </div>
      <div class="Page">
        <el-pagination @current-change="handlePage" :current-page.sync="currentPage" layout="prev, pager, next"
          :total="allList.length" :page-size="3">
        </el-pagination>
      </div>
      <!-- 发布消息资讯弹窗 -->
      <el-dialog title="发布消息资讯" :visible.sync="dialogForm" :before-close="handleClose" width="35%">
        <el-form :model="form" :rules="rules" ref="fbForm">
          <el-form-item label="回复内容" :label-width="'80px'" prop="reply">
            <el-input type="textarea" :rows="3" placeholder="请输入内容" v-model="form.reply">
            </el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="pushfb">发 送</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AdminFeedback',
  created() {
    this.getFeedbackList()
  },
  data() {
    return {
      //当前页
      currentPage: 1,
      // 反馈是否已被处理
      isHandled: false,
      // 反馈列表
      allList: [],
      list: [],
      //发布消息资讯弹窗
      dialogForm: false,
      //回复
      form: {
        reply: '',
        id: ''
      },
      rules: {
        reply: [
          { required: true, message: '请输入回复内容', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //获取反馈列表
    async getFeedbackList(page = 1) {
      const { data: res } = await this.$axios.get('/api/feedback/list')
      if (res.status == '200') {
        this.allList = res.data
        this.list = this.allList.slice(0, 3)
      }
      //之后再获取数据后不会跳回第一页而是保持在当前页
      this.handlePage(page)
    },
    //分页
    handlePage(page) {
      const start = (page - 1) * 3
      const end = page * 3
      this.list = this.allList.slice(start, end)
    },
    //点击取消或遮罩层关闭弹窗
    handleClose() {
      this.$refs['fbForm'].resetFields();
      this.form = {
        reply: '',
        id: '',
      }
      this.dialogForm = false
    },
    //点击回复反馈
    reply(id) {
      this.dialogForm = true
      this.form.id = id
    },
    //点击发送
    async pushfb() {
      this.$refs['fbForm'].validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.$axios.post('/api/feedback/reply', this.form)
          if (res.status == '200') {
            this.$message({
              message: '回复成功',
              type: 'success'
            })
          } else if (res.status == '400') {
            this.$message({
              message: res.message,
              type: 'error'
            })
          } else {
            this.$message({
              message: '回复失败',
              type: 'error'
            })
          }
          this.handleClose()
          this.getFeedbackList(this.currentPage)
        }
      });
    },
    //删除反馈
    async remoke(id) {
      const { data: res } = await this.$axios.get(`/api/feedback/delete/${id}`)
      if (res.status == '200') {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
      } else {
        this.$message({
          message: '删除失败',
          type: 'error'
        })
      }
      this.getFeedbackList(this.currentPage)
    }
  }
}
</script>

<style scoped>
.adminfeedback {
    width: 100%;
    min-height: calc(100vh - 60px);
    background-color: #f5f7fa;
    padding: 30px;
    box-sizing: border-box;
}

.adminmsg {
    width: 100%;
    background-color: #fff;
    border-radius: 12px;
    padding: 30px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.adminmsg h2 {
    font-size: 22px;
    font-weight: 600;
    color: #303133;
    margin-bottom: 25px;
    padding-bottom: 15px;
    border-bottom: 1px solid #ebeef5;
}

.Page {
    display: flex;
    justify-content: center;
    margin-top: 40px;
    padding-top: 20px;
    border-top: 1px solid #ebeef5;
}

.block {
    display: flex;
    align-items: center;
    width: 100%;
    flex-wrap: wrap;
    gap: 20px;
}

.title {
    height: 50px;
    background-color: #4b5fff;
    color: white;
    padding: 0 20px;
    margin: 25px 0 15px;
    border-radius: 8px;
    font-weight: 500;
    display: flex;
    align-items: center;
}

.title span {
    flex: 1;
}

.info {
    padding: 25px 0;
    border-bottom: 1px solid #ebeef5;
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    transition: all 0.3s ease;
}

.info:hover {
    background-color: #fafafa;
}

.info .msg {
    color: #606266;
    line-height: 1.6;
    flex: 1;
    min-width: 250px;
}

.status0 {
    color: #ffb358;
}

.status1 {
    color: green;
}

.msg h3 {
    font-size: 18px;
    font-weight: 600;
    color: #303133;
    margin-bottom: 8px;
}

.el-button {
    transition: all 0.3s;
}

.el-button:hover {
    transform: translateY(-2px);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.el-dialog {
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.el-dialog__title {
    font-size: 18px;
    font-weight: 600;
    color: #303133;
}

.el-dialog__footer {
    border-top: 1px solid #ebeef5;
    padding: 15px 20px;
    display: flex;
    justify-content: flex-end;
}

@media (max-width: 768px) {
    .adminfeedback {
        padding: 15px;
    }

    .adminmsg {
        padding: 20px;
    }
}
</style>